#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int** ret = (int**)malloc(sizeof(int*) * r);
    *returnColumnSizes = (int*)malloc(sizeof(int) * r);
    *returnSize = r;
    for (int i = 0; i < r; i++) {
        ret[i] = (int*)malloc(sizeof(int) * c);
        (*returnColumnSizes)[i] = c;
    }
    return ret;
}
int** matrixBlockSum(int** mat, int matSize, int* matColSize, int k, int* returnSize, int** returnColumnSizes) {
    int r = matSize;
    int c = matColSize[0];
    int** ret = myMalloc(r, c, returnSize, returnColumnSizes);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            int sum = 0;
            for (int m = i - k; m < i + 1 + k; m++) {
                for (int n = j - k; n < j + 1 + k; n++) {
                    if (m >= 0 && m < r && n >= 0 && n < c) {
                        sum += mat[m][n];
                    }
                }
            }
            ret[i][j] = sum;
        }
    }
    return ret;
}